package com.yjian.common;

import org.apache.commons.lang.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by xuxiaobao on 2017/9/27.
 */
public class DateUtil {
    private static SimpleDateFormat defaultDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    public static String dateToString(Date date, String pattern) {
        if (date == null) date = new Date();
        if (StringUtils.isEmpty(pattern)) {
           return defaultDateFormat.format(date);
        }
        return (new SimpleDateFormat(pattern)).format(date);
    }

    public static long dateToTimestamp(Date date) {
        if (date == null) date = new Date();
        return date.getTime();
    }

    public static Date timestampToDate(long timestamp) {
        return new Date(timestamp);
    }

    public static void main(String[] args) {
        System.out.println(dateToTimestamp(new Date()));
        System.out.println(dateToString(timestampToDate(1506501819415L),null));
    }
}
